﻿#include "CDeviceControlWidget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>

#include "CDeviceSettingsWidget.h"
#include "component/CPanelDashboard.h"
#include "component/CPanelDeviceList.h"
#include "component/CPanelExtraTools.h"
#include "component/CPanelLogger.h"
#include "core/CDeviceManager.h"
#include "core/logger.h"

CDeviceControlWidget::CDeviceControlWidget(QWidget *parent) :
    QWidget(parent),
    panel_logger_(new CPanelLogger(this)),
    panel_device_(new CPanelDeviceList(this)),
    panel_dashboard_(new CPanelDashboard(this)),
    panel_extra_(new CPanelExtraTools(this)),
    wnd_settings_(new CDeviceSettingsWidget(this))
{
    // layout
    auto lay1 = new QVBoxLayout;
    lay1->addWidget(panel_dashboard_, 1);
    lay1->addWidget(panel_logger_, 0);
    auto lay2 = new QVBoxLayout;
    lay2->addWidget(panel_device_, 1);
    lay2->addWidget(panel_extra_, 0);
    auto layout = new QHBoxLayout;
    layout->addLayout(lay2, 0);
    layout->addLayout(lay1, 1);
    // layout->addWidget(panel_toolbox_, 0);
    panel_device_->setMinimumWidth(250);
    panel_logger_->setMinimumHeight(250);
    panel_extra_->setMinimumHeight(250);
    setLayout(layout);
    setWindowTitle(tr("Device Control"));

    // properties
    wnd_settings_->setWindowFlag(Qt::Tool);
    wnd_settings_->hide();

    // connect
    connect(panel_device_, &CPanelDeviceList::deviceChanged, this, &CDeviceControlWidget::eventDeviceChanged);
    connect(wnd_settings_, &CDeviceSettingsWidget::deviceConfigChanged, this, &CDeviceControlWidget::eventDeviceConfigChanged);
    connect(panel_extra_, &CPanelExtraTools::configDevices, this, &CDeviceControlWidget::eventConfigDevices);
    connect(panel_extra_, &CPanelExtraTools::importConfigFile, this, &CDeviceControlWidget::eventImportConfigFile);
    connect(panel_extra_, &CPanelExtraTools::exportConfigFile, this, &CDeviceControlWidget::eventExportConfigFile);
}

CDeviceControlWidget::~CDeviceControlWidget()
{
}

void CDeviceControlWidget::SetUp()
{
    auto config = CDeviceManager::Instance().GetDeviceConfig();
    panel_device_->setDeviceConfig(config);
    panel_dashboard_->setDeviceConfig(config);
}

void CDeviceControlWidget::eventDeviceChanged(QString id)
{
    LOG(INFO) << tr("device control widget, device changed: ") << id;
    panel_dashboard_->eventDeviceChanged(id);
    panel_logger_->eventDeviceChanged(id);
}

void CDeviceControlWidget::eventConfigDevices()
{
    wnd_settings_->show();
    wnd_settings_->Reset();
}

void CDeviceControlWidget::eventDeviceConfigChanged()
{
    MSG(INFO) << tr("device config changed");
    auto config = CDeviceManager::Instance().GetDeviceConfig();
    panel_device_->setDeviceConfig(config);
    panel_dashboard_->setDeviceConfig(config);
    panel_dashboard_->eventDeviceChanged("");
}

void CDeviceControlWidget::eventImportConfigFile(QString filename)
{
    MSG(INFO) << tr("import config file: ") << filename;
    auto ret = CDeviceManager::Instance().LoadDeviceConfiguration(filename.toStdString());
    if (!ret)
    {
        MSG(ERROR) << tr("parse config file failed");
        return;
    }

    auto config = CDeviceManager::Instance().GetDeviceConfig();
    panel_device_->setDeviceConfig(config);
    panel_dashboard_->setDeviceConfig(config);
    panel_dashboard_->eventDeviceChanged("");
}

void CDeviceControlWidget::eventExportConfigFile(QString filename)
{
    MSG(INFO) << tr("export config file: ") << filename;
    auto ret = CDeviceManager::Instance().SaveDeviceConfiguration(filename.toStdString());
    if (!ret)
    {
        MSG(ERROR) << tr("save config file failed");
        return;
    }
    MSG(INFO) << tr("save config file success");
}